package com.itheima.service.impl;

import cn.itheima.health.service.OrderSettingService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class OrderSettingServiceImpl  implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> list) {
        //   如果用户重复上传文件 批量录入  对于同一天  最大预约数据累加或者更新处理
        for (OrderSetting orderSetting : list) {
            Date orderDate = orderSetting.getOrderDate();
            Long count = orderSettingMapper.findOrderSetingByOrderDate(orderDate);
            //  select   count(1) from  xxx  where orderdate = #{xxx}
            if(count>0){
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            }else{
                orderSettingMapper.add(orderSetting);
            }

        }

    }

    @Override
    public List<Map> findOrderSettingByDate(String beginDate, String endDate) {
        List<Map> map  = orderSettingMapper.findOrderSettingByDate(beginDate,endDate);

//        List<OrderSetting> orderSettingList = orderSettingMapper.findOrderSettingByDate(beginDate,endDate);
//        List<Map> map  = new ArrayList<Map>();
//        for (OrderSetting orderSetting : orderSettingList) {
//            Date orderDate = orderSetting.getOrderDate();
//            int date = orderDate.getDate();
//            Map m= new HashMap();
//            m.put("date",date);
//            m.put("number",orderSetting.getNumber());
//            m.put("reservations",orderSetting.getReservations());
//            map.add(m);
//        }
        return map;
    }

    @Override
    public void updateNumbersByDate(String date, Integer num) {
        orderSettingMapper.updateNumberValuesByOrderDate(date,num);
    }
}
